package com.situ.mvc.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mvc.common.ServerResponse;
import com.situ.mvc.entity.User;
import com.situ.mvc.service.IUserService;
import com.situ.mvc.service.impl.UserServiceImpl;
import com.situ.mvc.utill.JSONUtil;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/loginServlet")

public class LoginController {
	@Autowired
	private IUserService userService;

	@RequestMapping(value = "/logout")
	 private String logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		/*
		 * HttpSession session = req.getSession(); User user = (User)
		 * session.getAttribute("user");
		 * 
		 * if (user != null) { List<User> onLineUserList = (List<User>)
		 * req.getAttribute("onLineUserList"); onLineUserList.remove(user);
		 * 
		 * // session.removeAttribute("user"); session.invalidate();
		 * 
		 * // 重定向到登录界面 } }
		 */
	//  resp.sendRedirect(req.getContextPath() + "/login.jsp");
	  return "redirect:/login.jsp";
	  }
	
	
	
	

	@RequestMapping(value = "login")
	@ResponseBody
	private ServerResponse login(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String checkCode = req.getParameter("checkCode");
		// 将用户输入的验证码和后台生成的验证码比较
		String checkCodeSession = (String) req.getSession().getAttribute("checkCodeSession");
		if ("" == checkCode 
			|| null == checkCode 
			|| checkCode.equals(checkCodeSession) == false) {
			// resp.sendRedirect(req.getContextPath() + "/login.jsp");
			// return;
			
		ServerResponse serverResponse = ServerResponse.createError("验证码错误");
		//JSONUtil.obj2Json(resp, serverResponse);
		return serverResponse ;
		} 
		
		String name = req.getParameter("name");
		String password = req.getParameter("password");
		User user = userService.login(name, password);
		
		if (null != user) {// 登录成功
			HttpSession session = req.getSession();
			session.setAttribute("user", user);
			
			List<User> onLineUserList = (List<User>)session.getAttribute("onLineUserList");
			// 第一个用户登录时候这个list是null
			if (null == onLineUserList) {
				onLineUserList = new ArrayList<User>();
				session.setAttribute("onLineUserList", onLineUserList);
			}
			onLineUserList.add(user);
			
			//resp.sendRedirect(req.getContextPath() + "/student?mehtod=pageList");			
			ServerResponse serverResponse = ServerResponse.createSuccess("登录成功");
			
			//JSONUtil.obj2Json(resp, serverResponse);
			return serverResponse;
			
		} else {// 登录失败
			//resp.sendRedirect(req.getContextPath() + "/fail.jsp");
			ServerResponse serverResponse = ServerResponse.createError("用户名或密码错误");
			
			//JSONUtil.obj2Json(resp, serverResponse);
			return serverResponse;
		}
	}
		 
	}

